<?php
/**
 * 活动广告
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\AdverMod;
use Pub\cache\Cache;

class Adver extends Action
{
    private $Adver = null;
	public function __construct()
	{
		parent::__construct();
		$this->Adver = new AdverMod();
	}

    /**
     * 获取
     */
	public function find()
    {
        $_adver = $this->Adver->findOne(array('activity_id'=>$this->_gpc['activity_id']),array('id','start_adver','adver_url','interval_time','interval_unit','adver_image'));
        if(!$_adver){
            $this->Adver->insert(array(
                'activity_id'=>$this->_gpc['activity_id'],
                'interval_unit'=>'minute'
            ));
            $this->find();
        }else{
            $_adver['start_adver'] = $_adver['start_adver'] == 0 ? false : true;
        }
        $this->_result = array('status'=>'success','data'=>$_adver);
        $this->result($this->_result);
    }

    /**
     * 提交
     */
    public function submit()
    {
        if($this->_gpc['data']['start_adver'] == 'true'){
            $this->_gpc['data']['start_adver'] = 1;
        }else{
            $this->_gpc['data']['start_adver'] = 0;
        }
        if($this->Adver->update($this->_gpc['data'],array('activity_id'=>$this->_gpc['activity_id']))){
            Cache::remove(Cache::ACTIVITY_ADVER,array('activity_id'=>$this->_gpc['activity_id']));
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * 前台获取活动广告
     * @param activity_id 活动id
     * @param uid 用户id
     */
    public function frontFind()
    {
        $_activity_adver = $_activity_adver_cache = Cache::get(Cache::ACTIVITY_ADVER,array('activity_id'=>$this->_gpc['activity_id']));
        if(!$_activity_adver_cache){
            $_activity_adver = $this->Adver->findOne(array('activity_id'=>$this->_gpc['activity_id']),array('start_adver','adver_url','interval_time','interval_unit','adver_image'));
            Cache::set(Cache::ACTIVITY_ADVER,$_activity_adver,array('activity_id'=>$this->_gpc['activity_id']));
        }
        if($_activity_adver['start_adver'] == 1){
            $_adver_time = Cache::get(Cache::ACTIVITY_USER_ADVER_TIME,array('activity_id'=>$this->_gpc['activity_id'],'uid'=>$this->_gpc['uid']));
            if(!$_adver_time || ($_adver_time < TIMESTAMP) || ($_activity_adver['interval_time'] == 0)){
                if($_activity_adver['interval_time'] != 0){
                    $_adver_time = '';
                    if($_activity_adver['interval_unit'] == 'hour'){
                        $_adver_time = TIMESTAMP + $_activity_adver['interval_time'] * 3600;
                    }else{
                        $_adver_time = TIMESTAMP + $_activity_adver['interval_time'] * 60;
                    }
                    Cache::set(Cache::ACTIVITY_USER_ADVER_TIME,$_adver_time,array('activity_id'=>$this->_gpc['activity_id'],'uid'=>$this->_gpc['uid']));
                }
                $_data = array('src'=>tomedia($_activity_adver['adver_image']),'url'=>$_activity_adver['adver_url']);
                unset($_activity_adver);
                $this->_result = array('status'=>'success','data'=>$_data);
                $this->result($this->_result);
            }
        }
        $this->_result = array('status'=>'error');
        $this->result($this->_result);
    }
}